package com.cn.yugan.Activity;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.cn.yugan.Cons.LoginConstants;
import com.cn.yugan.mobile.R;
import com.cn.yugan.Utils.AccountType;


/**
 * Created by Rover on 2016/7/17 .
 * Email 357319517@qq.com
 * 提交成功-------->ChapasswordActivity
 * 登录失败-------->提示错误
 */

public class ForgetActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_name, et_captcha;
    private Button bt_getcaptcha, bt_submit;

    private int accountmode;

    private String account;
    private String captcha;


    Handler mhandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            bt_getcaptcha.setText(msg.obj.toString() + "秒");
            if ((int) msg.obj == 0) {
                bt_getcaptcha.setBackground(getResources().getDrawable(R.drawable.gray_btn_normal));
                bt_getcaptcha.setClickable(true);
                bt_getcaptcha.setText(getResources().getText(R.string.getcaptcha1));
            }
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget);
        init();


    }

    private void init() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("忘记密码");
        actionBar.setDisplayHomeAsUpEnabled(true);
        et_name = (EditText) findViewById(R.id.forget_username);
        et_captcha = (EditText) findViewById(R.id.forget_captcha);
        et_name.setImeOptions(EditorInfo.IME_ACTION_NEXT);
        et_captcha.setImeOptions(EditorInfo.IME_ACTION_NONE);
        et_name.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    et_captcha.requestFocus();
                    return true;
                }
                return false;
            }
        });
        et_captcha.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEND || (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    return true;
                }
                return false;
            }
        });
        bt_getcaptcha = (Button) findViewById(R.id.forget_getcaptcha);
        bt_submit = (Button) findViewById(R.id.forget_submit);


        bt_getcaptcha.setOnClickListener(this);
        bt_submit.setOnClickListener(this);


    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
                break;
        }
        return true;
    }
    //captcha读秒
    public void sendcaptcha() {
        bt_getcaptcha.setBackground(getResources().getDrawable(R.drawable.gray_btn_pressed));
        bt_getcaptcha.setClickable(false);
        new Thread(new Runnable() {
            @Override
            public void run() {
                int sec = 30;
                for (int i = 0; i < 31; i++) {
                    try {
                        Message msg = Message.obtain();
                        msg.obj = sec;
                        Thread.sleep(1000);
                        sec--;
                        mhandler.sendMessage(msg);
                    } catch (Exception e) {
                        Toast.makeText(ForgetActivity.this, "哎呀，YJ娘被爆菊了！", Toast.LENGTH_LONG).show();
                    }
                }
            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forget_getcaptcha: {
                //获取验证码
                //暂缺验证服务器接口
                //待补充获取验证码逻辑
                account = et_name.getText().toString().trim();
                if (account.equals("")){
                    Toast.makeText(this, "再不写账号，你就等着吧，哼(￢︿̫̿￢☆)", Toast.LENGTH_SHORT).show();
                }else if (AccountType.IsEmail(account)) {
                    //邮箱逻辑
                    accountmode = LoginConstants.LOGIN_EMAIL;
                    Toast.makeText(this, "邮箱", Toast.LENGTH_SHORT).show();
                    sendcaptcha();
                } else if (AccountType.IsPhone(account)) {
                    //手机逻辑
                    accountmode = LoginConstants.LOGIN_PHONE;
                    Toast.makeText(this, "手机", Toast.LENGTH_SHORT).show();
                    sendcaptcha();
                } else {
                    Toast.makeText(this, "欧尼酱,账号格式都不对啊？( ▼-▼ )", Toast.LENGTH_SHORT).show();
                }
                break;
            }

            case R.id.forget_submit: {
                captcha = et_captcha.getText().toString().trim();
                //暂缺验证服务器接口
                //待补充验证逻辑
                //提交验证码
                int status = 1; // 服务器返回值
                switch (accountmode) {
                    case LoginConstants.LOGIN_EMAIL:
                        if (status == 1) {
                            Intent intent = new Intent(ForgetActivity.this, ChapasswordActivity.class);
                            intent.putExtra("account", "邮箱地址:" + account);
                            startActivity(intent);
                            ForgetActivity.this.finish();
                        } else {
                            Toast.makeText(ForgetActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    case LoginConstants.LOGIN_PHONE:
                        if (status == 1) {
                            Intent intent = new Intent(ForgetActivity.this, ChapasswordActivity.class);
                            intent.putExtra("account", "手机号:" + account);
                            startActivity(intent);
                            ForgetActivity.this.finish();
                        } else {
                            Toast.makeText(ForgetActivity.this, "验证码错误", Toast.LENGTH_SHORT).show();
                        }
                        break;
                    default:
                        Toast.makeText(this,"请检查输入的信息",Toast.LENGTH_SHORT).show();
                }
                break;
            }
        }

    }
}
